/*
 * Bindings.scala
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.squela

import scala.collection.mutable.Map

class Bindings extends Iterable[(Field[_], Bindings.ClassField[_, _])] {
    import Bindings._

    val map = Map[Field[_], ClassField[_, _]]()

    def += [T, O](f: Field[T], classField: ClassField[O, T]) {map += Pair(f, classField)}

    def + [T, O](f: Field[T], classField: ClassField[O, T]) = {
        +=(f, classField)
        this
    }

    def iterator = map.iterator
    def contains(f: Field[_]) = map.contains(f)
    def apply[T, O](f: Field[T]): ClassField[O, T] = map(f).asInstanceOf[ClassField[O, T]]
    def update(f: Field[_], classField: ClassField[_, _]) = map(f) = classField
}

object Bindings {
    class ClassField[T, R] private (val getter: (T) => R, val setter: (T, R) => Unit)
    object ClassField {
        def apply[T, R](getter: (T) => R, setter: (T, R) => Unit) = new ClassField(getter, setter)
    }
}